//
//  CRProfileController.m
//  CRChatRoom
//
//  Created by CuiFengshuai on 15/11/17.
//  Copyright © 2015年 SecondGroup. All rights reserved.
//

#import "CRProfileController.h"
#import "ParallaxHeaderView.h"
#import "CRSetController.h"
#import "CRProfileTableViewCell.h"
#import "CREditInformationController.h"
#import "CRUserDetailModel.h"
@interface CRProfileController () <UITableViewDataSource>

@property (nonatomic, strong)UIImageView *contentLineImageView;
@end

@implementation CRProfileController


#pragma mark >>>> 懒加载

#pragma mark >>>> 系统方法

- (void)viewDidLoad {
    [super viewDidLoad];
    // 自定义控件初始化
    [self setupSubViews];
    
    // 初始化数据模型
    [self initData];
    
   
    
    
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _contentLineImageView.hidden = YES;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    _contentLineImageView.hidden = NO;
}




#pragma mark >>>> 自定义控件方法
/**
 *  自定义控件初始化
 */
- (void)setupSubViews {
    // 设置背景图片
    [self setBackImageView:@"back_liangshenghua"];
    // 设置tableView
    [self addTabelView];
    // 设置导航栏颜色透明
    [self setnavigationBarClear];
    // 添加bauButtonItem
    [self addBarButtonItem];
}

- (void)addBarButtonItem {
    
    UIButton *button = [[UIButton alloc] init];
    button.bounds = CGRectMake(0, 0, 30, 30);
    [button setBackgroundImage:[UIImage imageNamed:@"backNormal"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"baclhigh"] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(didLeftBarButtonItem) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    
    UIButton *Rbutton = [[UIButton alloc] init];
    Rbutton.bounds = CGRectMake(0, 0, 30, 30);
    [Rbutton setBackgroundImage:[UIImage imageNamed:@"bianji_normal"] forState:UIControlStateNormal];
    [Rbutton setBackgroundImage:[UIImage imageNamed:@"bianji_click"] forState:UIControlStateHighlighted];
    [Rbutton addTarget:self action:@selector(didClickRightBarButtonItem) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:Rbutton];
    

    
}

/**
 *  设置背景图片
 *
 *  @param imageName 图片名
 */
- (void)setBackImageView:(NSString *)imageName {
    // 初始化一个背景imageView
    UIImageView *backImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
//    CGFloat backImageViewX = 0;
//    CGFloat backImageViewY = 0;
//    CGFloat backImageViewW = self.view.width;
//    CGFloat backImageViewH = self.view.height;
//    backImageView.frame = CGRectMake(backImageViewX, backImageViewY, backImageViewW, backImageViewH);
    backImageView.frame = self.view.bounds;
    
    [self.view addSubview:backImageView];
}
/**
 *  添加tableView
 */
- (void)addTabelView {
    
    UITableView *tabelView = [[UITableView alloc] init];
    CGFloat tabelViewX = 10;
    CGFloat tabelViewY = 0;
    
    CGFloat tabelViewW = self.view.width - 2 * tabelViewX;
    CGFloat tabelViewH = self.view.height;
    tabelView.frame = CGRectMake(tabelViewX, tabelViewY, tabelViewW, tabelViewH);
    tabelView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    // 设置单元格分割线
    tabelView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    tabelView.dataSource = self;
    // 设置tableView的 contentInset
    tabelView.contentInset = UIEdgeInsetsMake(self.view.height - kDefaultHeaderViewHeight, 0, 0, 0);
    
    ParallaxHeaderView *headerView = [ParallaxHeaderView parallaxHeaderViewWithImageURL:self.userDetailM.iconUrl name:self.userDetailM.username age:self.userDetailM.age gender:self.userDetailM.gender forSize:CGSizeMake(tabelView.width, kDefaultHeaderViewHeight)];
    
    [tabelView setTableHeaderView:headerView];
    
    [self.view addSubview:tabelView];
}

/**
 *  设置导航栏颜色透明
 */
- (void)setnavigationBarClear {
    
    // 随便给一张图片
    UIImage *image = [UIImage imageNamed:@"buddy_header_bg_highlighted"];
    // 设置navigationBar forBarMetrics:UIBarMetricsCompact
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsCompact];
    // 调用findHairlineImageViewUnder 方法
    _contentLineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
}

/**
 *  初始化数据模型
 */
- (void)initData {
    
}

#pragma mark >>>> 事件监听方法
- (void)didClickLeftBarButtonItem:(UIBarButtonItem *)item {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)didClickRightBarButtonItem
{
    CREditInformationController *editVC = [[CREditInformationController alloc] init];
    editVC.userDetailM = self.userDetailM;
    [self.navigationController pushViewController:editVC animated:NO];
    
}
#pragma mark >>>> 实现代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CRProfileTableViewCell *cell = [CRProfileTableViewCell cellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    AVUser *currentUser = [AVUser currentUser];
    
    switch (indexPath.row) {
        case 0: {
            cell.titleLabel.text = @"账号信息";
            cell.detailLabel.text = currentUser.username;
            break;
        }
        case 1:{
            cell.titleLabel.text = @"性别";
            cell.detailLabel.text = self.userDetailM.gender;
            break;
        }
        case 2:{
            cell.titleLabel.text = @"生日";
            cell.detailLabel.text = self.userDetailM.brithday;
            break;
        }
        case 3:{
            cell.titleLabel.text = @"工作";
            cell.detailLabel.text = self.userDetailM.job;
            break;
        }
        case 4:{
            cell.titleLabel.text = @"家乡";
            cell.detailLabel.text = self.userDetailM.hometown;
            break;
        }
        case 5:{
            cell.titleLabel.text = @"兴趣";
            cell.detailLabel.text = self.userDetailM.hobby;
            break;
        }
        default:
            break;
    }
    
    return cell;
    
}

#pragma mark >>>> 其他方法
/**
 *  是导航栏下面的黑线消失
 *
 */
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}


#pragma mark - 左上角返回按钮
- (void)didLeftBarButtonItem
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
